package com.shop.cereshop.agent.controller.referral;

import com.shop.cereshop.agent.page.referral.AgentGetIncomeVO;
import com.shop.cereshop.agent.page.referral.AgentShopDetailVO;
import com.shop.cereshop.agent.page.referral.AgentShopOrderDetailVO;
import com.shop.cereshop.agent.page.referral.AgentShopsVO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.service.referral.AgentReferralService;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 代理商推广关系控制器
 * @author
 * @date
 * @version 1.0
 */
@RestController
@RequestMapping("/referral")
@Api(value = "AgentReferralController", tags = "推广关系")
public class AgentReferralController {

  @Resource
  private AgentReferralService agentReferralService;

  @GetMapping("/get/income")
  @ApiOperation(value = "获取代理收益接口")
  public Result<AgentGetIncomeVO> getIncome(HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(agentReferralService.getIncome(user.getAgentUserId()));
  }

  @GetMapping("/shops")
  @ApiOperation(value = "获取代理商辖区下的店铺列表接口")
  public Result<AgentShopsVO> getShops(@RequestParam("page") @ApiParam(value = "页码", required = true) Integer page,
      @RequestParam("pageSize") @ApiParam(value = "页大小", required = true) Integer pageSize,
      @RequestParam("shopType") @ApiParam(value = "店铺类型 1-线上商户 2-线下门店", required = true) Integer shopType,
      @RequestParam(value = "search", required = false) @ApiParam(value = "搜索关键字") String search,
      @RequestParam(value = "sort", required = false) @ApiParam(value = "排序字段 1-营业额 2-订单量 不传按默认排序") Integer sort,
      @RequestParam(value = "timeRange", required = false) @ApiParam(value = "时间范围 1-本周 2-本月 3-本年 不传默认为本月") Integer timeRange,
      HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(
        agentReferralService.getShops(user.getAgentUserId(), page, pageSize, shopType, search, sort, timeRange));
  }

  @GetMapping("/shop/detail")
  @ApiOperation(value = "获取代理商辖区下的店铺详情接口")
  public Result<AgentShopDetailVO> getShopDetail(
      @RequestParam("shopId") @ApiParam(value = "店铺ID", required = true) Long shopId,
      @RequestParam("page") @ApiParam(value = "页码", required = true) Integer page,
      @RequestParam("pageSize") @ApiParam(value = "页大小", required = true) Integer pageSize,
      @RequestParam(value = "month", required = false) @ApiParam(value = "月份筛选 格式：2025-10，不传则查询全部") String month,
      HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(agentReferralService.getShopDetail(user.getAgentUserId(), shopId, page, pageSize, month));
  }

  @GetMapping("/shop/order/detail")
  @ApiOperation(value = "获取代理商辖区下的店铺订单详情接口")
  public Result<AgentShopOrderDetailVO> getShopOrderDetail(
      @RequestParam("orderId") @ApiParam(value = "订单ID", required = true) Long orderId,
      HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(agentReferralService.getShopOrderDetail(user.getAgentUserId(), orderId));
  }

}